package com.fengba.nanzhi.setup;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.Preferences;
import com.fengba.nanzhi.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by SpakeBrony on 2018/4/9.
 */

public class SetUp extends AppCompatActivity {

    //菊花
    private ProgressBar SUjuhua;

    public SetUp() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_set_up);
        SUjuhua = (ProgressBar) findViewById(R.id.SUjuhua);


        /**
         * 返回
         */
        ImageButton SUreturn = (ImageButton) findViewById(R.id.SUreturn);
        SUreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        /**
         * 修改密码
         */
        Button SUbtn01 = (Button) findViewById(R.id.SUbtn01);
        SUbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (FX_SignInUtil.switchAccount(SetUp.this)) {
                    startActivity(new Intent(SetUp.this, Login.class));
                } else {
                    Intent intent = new Intent(SetUp.this, ForGet02.class);
                    startActivity(intent);
                }
            }
        });


        /**
         * 修改手机号
         */
        Button SUbtn02 = (Button) findViewById(R.id.SUbtn02);
        SUbtn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (FX_SignInUtil.switchAccount(SetUp.this)) {
                    startActivity(new Intent(SetUp.this, Login.class));
                } else {
                    Intent intent = new Intent(SetUp.this, ForPhone.class);
                    startActivity(intent);
                }
            }
        });

        /**
         * 关于疯吧
         */
        Button SUbtn03 = (Button) findViewById(R.id.SUbtn03);
        SUbtn03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SetUp.this, AboutUs.class);
                startActivity(intent);
            }
        });

        /**
         * 意见提交
         */
        Button SUbtn04 = (Button) findViewById(R.id.SUbtn04);
        SUbtn04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SetUp.this, Opinion.class);
                startActivity(intent);
            }
        });

        /**
         * 服务协议
         */
        Button SUbtn05 = (Button) findViewById(R.id.SUbtn05);
        SUbtn05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SetUp.this, com.fengba.nanzhi.login.Yhxy.class);
                intent.putExtra("id", 1);
                startActivity(intent);
            }
        });

        /**
         * 退出登录
         */
        Button SUbtn07 = (Button) findViewById(R.id.SUbtn07);
        SUbtn07.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (FX_SignInUtil.switchAccount(SetUp.this)) {
                    startActivity(new Intent(SetUp.this, Login.class));
                } else {
                    setBasicSetup(2);
                    setBasicSetup(4);

                    AlertDialog dialog = new AlertDialog.Builder(SetUp.this)
                            .setTitle("退出登录")//设置对话框的标题
                            .setMessage("是否退出登录")//设置对话框的内容
                            //设置对话框的按钮
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                                    dialog.dismiss();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                    Preferences.clearAll(SetUp.this);
                                    dialog.dismiss();
                                }
                            }).create();
                    dialog.show();

                }
            }
        });

       /* *//**
         * 注销账号
         *//*
        Button SUbtn06 = (Button) findViewById(R.id.SUbtn06);
        SUbtn06.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog dialog = new AlertDialog.Builder(SetUp.this)
                        .setTitle("注销账号")//设置对话框的标题
                        .setMessage("是否注销账号")//设置对话框的内容
                        //设置对话框的按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                                //加载
                                SUjuhua.setVisibility(ProgressBar.VISIBLE);

                                OkHttpClient client = new OkHttpClient();
                                //构建FormBody，传入要提交的参数
                                FormBody formBody = new FormBody
                                        .Builder()
                                        .add("code", "测试")
                                        .build();
                                final Request request = new Request.Builder()
                                        .url(UrlUtil.UrlFX + "/api/user/save")
                                        .post(formBody)
                                        .build();
                                Call call = client.newCall(request);
                                call.enqueue(new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        ToastUtil.showToast(SetUp.this, "网络错误", SUjuhua);
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        try {
                                            String json = response.body().string();
                                            JSONObject jsonObject = new JSONObject(json);
                                            final String message = (String) jsonObject.get("message");
                                            if (jsonObject.get("code").equals(0)) {
                                                JSONObject data = (JSONObject) jsonObject.get("data");
                                                Intent intent = new Intent(SetUp.this, Login.class);
                                                startActivity(intent);
                                            } else {
                                                ToastUtil.showToast(SetUp.this, message, SUjuhua);
                                            }

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });


                                //Toast.makeText(MainActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        }).create();
                dialog.show();
            }
        });*/

    }//都在这里打
    /**
     * 1-2-3-4
     * 增、删、改、查
     */
    public void setBasicSetup(int type) {
        String account = null;
        try {
            Object user_info = Preferences.getParam(SetUp.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            account = myJson.get("account").toString();
            //System.out.println("account"+account);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (type == 1) {
            //设置别名（新的调用会覆盖之前的设置）
            JPushInterface.setAlias(this, 0, account);
            //设置标签（同上）
            JPushInterface.setTags(this, 0, setUserTags(account));
        } else if (type == 2) {
            //删除别名
            JPushInterface.deleteAlias(this, 0);
            //删除指定标签
            JPushInterface.deleteTags(this, 0, setUserTags(account));
            //删除所有标签
            //JPushInterface.cleanTags(this, 0);
        } else if (type == 3) {
            //增加tag用户量(一般都是登录成功设置userid为目标，在别处新增加比较少见)
            //JPushInterface.addTags(this, 0, setUserTags());
        } else if (type == 4) {
            //查询所有标签
            JPushInterface.getAllTags(this, 0);
            //查询别名
            JPushInterface.getAlias(this, 0);
            //查询指定tag与当前用户绑定的状态（MyJPushMessageReceiver获取）
            JPushInterface.checkTagBindState(this, 0, account);
            //获取注册id
            JPushInterface.getRegistrationID(this);
        }
    }

    /**
     * 标签用户
     * @return
     */

    private static Set<String> setUserTags(String account) {
        //添加3个标签用户（获取登录userid较为常见）
        Set<String> tags = new HashSet<>();
        tags.add(account);
        return tags;
    }


}